# -*- coding: utf-8 -*-
# @Time    : 2024/5/20 18:27
# @Author  : Alvin
# @File    : crawl_service.py
from flask import Blueprint, request, jsonify,g
from ..utils.redis_service import get_progress
from ..utils.scrapyd_job_operation import cancel_scrapyd_job
crawl_bp = Blueprint('crawl_bp',__name__)
@crawl_bp.route("/get_progress", methods=['GET'] )
def get_progress_api():
    job_id = request.args.get('jobId')
    result = get_progress(job_id)
    if result:
        return jsonify({'status':200, 'data':result}), 200
    else:
        return jsonify({'status':500, 'data':"获取进度失败"}), 500
@crawl_bp.route("/cancel_task", methods=['GET'])
def cancel_task_by_job_id():
    job_id = request.args.get('jobId')
    data = {
        'project':'scrapy',
        'job':job_id
    }
    response = cancel_scrapyd_job(data)
    if response:
        if response['status'] == 'ok':
            return jsonify({'status':200, 'data':"任务取消成功!"}), 200
        else:
            return jsonify({'status':500, ' data':'任务取消失败!'}), 500
    return jsonify({'status': 500, ' data': 'scrapyd出错了!'}), 500


